package com.iemes.controller.device;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DeviceFormMap;
import com.iemes.entity.DeviceTypeFormMap;
import com.iemes.mapper.DeviceMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Classname DeviceManageController
 * @Description TODO
 * @Date 2022/4/14 18:32
 * @Created by You
 */
@Controller
@RequestMapping("/devicemanagemt/")
public class DeviceManagemtController extends BaseController {
    @Inject
    private UserMapper userMapper;

    @Inject
    private DeviceMapper deviceMapper;

    @RequestMapping("devicemanagemt")
    public String listUI(Model model) throws Exception {
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/device/managemt/devicemanagemt_list";
    }


    @ResponseBody
    @RequestMapping("devicemanagemt_findByPage")
    public PageInfo findByPage(String pageNow,
                               String pageSize, HttpServletRequest request) throws Exception {

        //采用pageinfo分页
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }
        String paramName = getPara("paramName");
        String paramValue =  getPara("paramValue");

        DeviceTypeFormMap deviceTypeFormMap=getFormMap(DeviceTypeFormMap.class);
        toFormMap(deviceTypeFormMap,pageNow,pageSize,deviceTypeFormMap.getStr("orderby"));

        List<DeviceFormMap> deviceFormMaps=deviceMapper.getDeviceManage(paramName,paramValue);
        PageInfo pageInfo = new PageInfo<>(deviceFormMaps);
        return pageInfo;
    }

    /**
     * 删除
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("devicemanagemt_deleteEntity")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备管理",methods="删除设备")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delDeviceType() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            userMapper.deleteByAttribute("deviceId", id, DeviceFormMap.class);
        }
        return "success";
    }


    /**
     * 跳转到新增页面
     */
    @RequestMapping("devicemanagemt_addUI")
    public String addUI(Model model) throws Exception {
        model.addAttribute("list",deviceMapper.getDeviceType());
        model.addAttribute("listClient",deviceMapper.getClient());
        return Common.BACKGROUND_PATH + "/device/managemt/devicemanagemt_add";
    }

    /**
     * 校验设备是否存在
     * @return
     */
    @ResponseBody
    @RequestMapping("devicemanagemt_isExsit")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备管理",methods="设备校验")//凡需要处理业务逻辑的.都需要记录操作日志
    public String isExsit(HttpServletRequest request){

        String deviceCode = request.getParameter("deviceCode");

        DeviceFormMap deviceFormMap = deviceMapper.getDeviceManageByCode(deviceCode);
        if(deviceFormMap == null){
            return "none";
        }
        return "success";
    }


    /**
     * 新增
     * @return
     */
    @ResponseBody
    @RequestMapping("devicemanagemt_addEntity")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备管理",methods="设备新增")//凡需要处理业务逻辑的.都需要记录操作日志
    public String addEntity(){
        DeviceFormMap deviceFormMap = getFormMap(DeviceFormMap.class);
        try {
            userMapper.addEntity(deviceFormMap);//新增后返回新增信息
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }


    @RequestMapping("devicemanagemt_editUI")
    public String editUI(Model model, HttpServletRequest request) throws Exception {
        //获取供应商
        model.addAttribute("listClient",deviceMapper.getClient());
        //获取设备种类
        model.addAttribute("list",deviceMapper.getDeviceType());

        String id = getPara("id");
        if(Common.isNotEmpty(id)){
            model.addAttribute("user",userMapper.findbyFrist("deviceId", id, DeviceFormMap.class));
        }
        return Common.BACKGROUND_PATH + "/device/managemt/devicemanagemt_edit";
    }


    @ResponseBody
    @RequestMapping("devicetype_editEntity")
    @SystemLog(module="设备种类管理",methods="设备种类编辑")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity() throws Exception {

        DeviceFormMap deviceFormMap=getFormMap(DeviceFormMap.class);
//        deviceTypeFormMap.put("txtGroupsSelect", txtGroupsSelect);
        userMapper.editEntity(deviceFormMap);
        return "success";
    }


}
